package viewer;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.ShareFileModel;
import server.LocalServer;

import common.ClientModel;
import common.FileUpNotifyQueue;
import common.Server;

public class ServerFrame extends JFrame implements ActionListener, WindowListener{

	private static final long serialVersionUID = 3729514504823371917L;

	Server server;
	
	public JLabel selectFileLbl = new JLabel("请选择文件:");
	public JTextField filePathTxt = new JTextField(20);
	
	public JLabel fileNameLbl = new JLabel("共享文件名:");
	public JTextField fileNameTxt = new JTextField(20);
	
	public JLabel encodeLbl = new JLabel("文件编码:");
	public JTextField encodeTxt = new JTextField(10);
	
	public JButton shareBtn = new JButton("共享");
	public JButton showClientBtn = new JButton("当前连接client");
	public JButton showShareFilestBtn = new JButton("当前共享files");
	public JButton showUpFileQueueBtn = new JButton("当前文件更新通知队列");
	public JButton clearBtn = new JButton("clear message");
	public JButton closeWindowBtn = new JButton("关闭");
	public JTextArea messageArea = new JTextArea(50, 60);
	public JScrollPane scrollPane = new JScrollPane(messageArea);
	
	public ServerFrame() {
		init();
		createEvent();
	}
	private void createEvent() {
		
		shareBtn.addActionListener(this);
		showClientBtn.addActionListener(this);
		showUpFileQueueBtn.addActionListener(this);
		showShareFilestBtn.addActionListener(this);
		closeWindowBtn.addActionListener(this);
		clearBtn.addActionListener(this);
		this.addWindowListener(this);
	}
	private void init() {
		
		Integer port = Integer.parseInt(Server.rb.getString("port"));
		System.out.println("Startuping Log server........");
		server = new LocalServer();
		server.startup(port);
		System.out.println(new Date());
		System.out.println("Log server startuping in port " + port + "..........");
		
		/*JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setMultiSelectionEnabled(false);
		fileChooser.setCurrentDirectory(null);
		fileChooser.setApproveButtonText("OK");*/
		//fileChooser.showDialog(this, "select file");
		
		messageArea.setEditable(false);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		Container currFrame = this.getContentPane();
		currFrame.setLayout(new FlowLayout());
		currFrame.add(selectFileLbl);
		currFrame.add(filePathTxt);
//		currFrame.add(fileChooser);
		currFrame.add(fileNameLbl);
		currFrame.add(fileNameTxt);
		currFrame.add(encodeLbl);
		currFrame.add(encodeTxt);
		currFrame.add(shareBtn);
		currFrame.add(showClientBtn);
		currFrame.add(showShareFilestBtn);
		currFrame.add(showUpFileQueueBtn);
		currFrame.add(clearBtn);
		currFrame.add(closeWindowBtn);
		currFrame.add(scrollPane);
		
		this.setSize(600, 500);
		this.setTitle("Log Server");
		this.setDefaultCloseOperation(3);
//		this.setExtendedState(MAXIMIZED_BOTH);
		this.setEnabled(true);
		this.setVisible(true);
	}

	@Override
	public void windowClosing(WindowEvent window) {
		closeWindow();
		
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		
		Object btnObj = event.getSource();
		if (btnObj.equals(shareBtn)) {
			int opt = JOptionPane.showConfirmDialog(this, "Share this file ?", "Warning", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
			if (opt == JOptionPane.YES_OPTION) {
				ShareFileModel shareFile = new ShareFileModel(filePathTxt.getText(), fileNameTxt.getText(), encodeTxt.getText());
				// TODO 添加共享文件时通知clients
				server.addShareFile(shareFile);
				JOptionPane.showMessageDialog(this, "共享文件成功");
			}
		}
		if (btnObj.equals(showClientBtn)) {
			messageArea.append("\12当前客户端连接: " + server.getListClient().size() + "\12");
			for (ClientModel client : server.getListClient()) {
				messageArea.append("ip: " + client.getAddress() + ", wait file update notify port: " + client.getPort() + "\12");
			}
		}
		if (btnObj.equals(showUpFileQueueBtn)) {
			messageArea.append("\12当前文件更新通知队列: " + server.getListNotifyQueue().size() + "\12");
			for (FileUpNotifyQueue upNotifyQueue : server.getListNotifyQueue()) {
				messageArea.append("ip: " + upNotifyQueue.getClient().getAddress() 
						+ ", port: " + upNotifyQueue.getClient().getPort() 
						+ ", \12file path: " + upNotifyQueue.getShareFileModel().getServerFilePath()
						+ ", file name: " + upNotifyQueue.getShareFileModel().getFileName()
						+ "\12");
			}
		}
		if (btnObj.equals(showShareFilestBtn)) {
			messageArea.append("\12当前共享文件: " + server.getListShareFile().size() + "\12");
			for (ShareFileModel shareFileModel : server.getListShareFile()) {
				messageArea.append("file path: " + shareFileModel.getServerFilePath()
						+ ", file name: " + shareFileModel.getFileName()
						+ "\12");
			}
		}
		if (btnObj.equals(clearBtn)) {
			messageArea.setText("");
		}
		if (btnObj.equals(closeWindowBtn)) {
			closeWindow();
		}
	}
	
	@Override
	public void windowActivated(WindowEvent arg0) {
		
	}
	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void windowOpened(WindowEvent arg0) {
		
	}
	
	private void closeWindow() {
		int opt = JOptionPane.showConfirmDialog(this, "Exit ?", "Warning", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
		if (opt == JOptionPane.YES_OPTION) {
			server.shutdown();
			System.out.println("Server log out!");
			System.exit(0);
		}
	}
}
